<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" name="" id="" />

    <script>
      // 函数防抖：1. 函数频繁触发，2. 前面的执行的函数无意义
      function debounce(fn, timeOut) {
        let timer = null;
        return function (...rest) {
          // 用户有执行函数，将之前的函数取消
          if (timer) {
            clearInterval(timer);
            timer = null;
          }
          timer = setTimeout(() => {
            fn.apply(this, rest);
          }, timeOut);
        };
      }

      const inpEl = document.querySelector("input");
      const fn = debounce(function (e) {
        console.log("用户有按键", this.value, e);
      }, 2000);
      inpEl.addEventListener("keyup", fn);
    </script>
  </body>
</html>
